﻿using PC.PickleJar.Configuration;
using PC.PickleJar.Exceptions;
using PC.PickleJar.Validators;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PC.PickleJar.Helpers
{
	public class ConfigurationHelper
	{
		private PickleJarSection configSection;

		public ConfigurationHelper()
		{
			this.configSection = (PickleJarSection)ConfigurationManager.GetSection(PickleJarSection.SectionName);

			if (this.configSection == null)
			{
				throw new ConfigurationNotFoundException();
			}
		}

		public IEnumerable<ValidatorBase> Validators
		{
			get
			{
				return this.configSection.Validators.Cast<ValidatorElement>().Select(item => item.ValidatorInstance).ToList();
			}
		}

		public ValidationFailureActionsEnum ValidationFailureAction
		{
			get
			{
				var result = ConfigurationDefaults.ValidationFailureAction;

				if (Enum.IsDefined(typeof(ValidationFailureActionsEnum), this.configSection.OnValidationFailure.Action))
				{
					result = this.configSection.OnValidationFailure.Action;
				}

				return result;
			}
		}
	}
}
